<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebSocket</title>
</head>
<body>
  <p>
      <label for="message"></label>
      <input type="text" id="message" placeholder="输入消息"/>
      <button type="button" onclick="sendMessage()">发送</button>
  </p>
  <div id="status"></div>
  <script>
    const msg = document.getElementById("message");
    const status = document.getElementById("status");
    const socket = new WebSocket("ws://localhost:8080/ws");

    socket.onopen = () => {
      status.innerHTML += "<p>状态: 已连接</p>";
    };

    socket.onclose = () => {
      status.innerHTML += "<p>状态: 断开连接</p>";
    };

    socket.onerror = (e) => {
      status.innerHTML += "<p>错误: " + e.message + "</p>";
    };

    const sendMessage = () => {
      socket.send(msg.value);
      msg.value = "";
    }
  </script>
</body>
</html>